/**
 * Copyright (c) 2014-now, secondriver (secondriver@yeah.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package secondriver.jfinal.rack.pugin.sql;

import org.apache.log4j.Logger;

import com.jfinal.plugin.IPlugin;

public class SqlManagerPlugin implements IPlugin {

	private static Logger logger = Logger.getLogger(SqlManagerPlugin.class);

	private String sqlFile = "sql.xml";

	public SqlManagerPlugin() {
	}

	public SqlManagerPlugin(String sqlFile) {
		if (null != sqlFile && "".equals(sqlFile)) {
			this.sqlFile = sqlFile;
		}
	}

	public boolean start() {
		if (logger.isInfoEnabled()) {
			logger.info("SqlManagerPlugin start loading.");
		}
		SqlParse sqlPare = new SqlParse(sqlFile);
		boolean result = sqlPare.isSucceed();
		if (!result) {
			logger.error("SqlManagerPlugin start failed, Please check your "
					+ sqlFile + " file.");
		} else {
			if (logger.isInfoEnabled()) {
				logger.info("SqlManagerPlugin start load sqlFile '" + sqlFile
						+ "' total <" + SqlKit.size() + "> lines.");
			}
		}
		return result;
	}

	public boolean stop() {
		SqlKit.clear();
		if (logger.isInfoEnabled()) {
			logger.info("SqlManagerPlugin stop.");
		}
		return true;
	}

}
